package GUI;

import Listeners.BuyBookFrameListener;
import Listeners.CloseBookInfoFrame;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 *
 */
public class SearchBookFrame extends JFrame{

    static JFrame bookInfo = new JFrame("Book Information");
    char bar;
    static char rBar;
    int barCount = 0;
    static String ID = "";
    String author = "";
    static String title = "";
    String cat = "";
    static String price = "";
    String avail = "";

    JPanel top = new JPanel(new GridLayout(0,1));
    JPanel content = new JPanel(new GridLayout(6,1));
    JPanel bottom = new JPanel(new GridLayout(0,2));
    JButton buy = new JButton("Buy Book");
    JButton close = new JButton("Close");

    public SearchBookFrame(String message){
     System.out.println(MainFrame.state);
   // MainFrame.state = message.charAt(0);
    for(int i = 0; i < message.length(); i++)
    {
        bar = message.charAt(i);
        if(bar == '|')
        {
             barCount++;
        }
        if(barCount == 2)
        {
             ID += bar;
        }
        if(barCount == 3)
        {
             author += bar;
        }
        if(barCount == 4)
        {
            title += bar;
        }
        if(barCount == 5)
        {
            cat += bar;
        }
        if(barCount == 6)
        {
            price += bar;
        }
        if(barCount == 7)
        {
            avail += bar;
        }
    }
        ID = ID.substring(1,ID.length());
        author = author.substring(1,author.length());
        title = title.substring(1,title.length());
        cat = cat.substring(1,cat.length());
        price = price.substring(1,price.length());
        avail = avail.substring(1,avail.length());

        setup();

       bookInfo.setSize(400,500);

       bookInfo.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

       bookInfo.setLocationRelativeTo(null);

       bookInfo.setVisible(true);

    }
    private void setup(){

        close.addActionListener(new CloseBookInfoFrame());
        buy.addActionListener(new BuyBookFrameListener());

        JLabel bName = new JLabel("Book Title:     " + title);
        bName.setFont(new Font("Calibri", Font.BOLD,20));
        bName.setForeground(Color.BLUE);
        bName.setSize(400, 15);

        JLabel bAuthor = new JLabel("Author Name:    " + author);
        bName.setFont(new Font("Calibri", Font.BOLD,20));
        bName.setForeground(Color.BLUE);

        JLabel bCategory = new JLabel("Category:    " + cat);
        bName.setFont(new Font("Calibri", Font.BOLD,20));
        bName.setForeground(Color.BLUE);

        JLabel bISBN = new JLabel("Book ID:    " + ID);
        bName.setFont(new Font("Calibri", Font.BOLD,20));
        bName.setForeground(Color.BLUE);

        JLabel bPrice = new JLabel("Book Price:    " + price);
        bName.setFont(new Font("Calibri", Font.BOLD,20));
        bName.setForeground(Color.BLUE);

        JLabel bookAvail = new JLabel("Book Availability:    " + avail);
        bName.setFont(new Font("Calibri", Font.BOLD,20));
        bName.setForeground(Color.BLUE);

       /* bName.setHorizontalAlignment(SwingConstants.RIGHT);
        bAuthor.setHorizontalAlignment(SwingConstants.RIGHT);
        bCategory.setHorizontalAlignment(SwingConstants.RIGHT);
        bISBN.setHorizontalAlignment(SwingConstants.RIGHT);
        bYear.setHorizontalAlignment(SwingConstants.RIGHT);
        bPrice.setHorizontalAlignment(SwingConstants.RIGHT);   */

        top.add(bName);

        content.add(bAuthor);
        content.add(bCategory);
        content.add(bISBN);
        content.add(bookAvail);
        content.add(bPrice);

        bottom.add(buy);
        bottom.add(close);


        bookInfo.add(top,BorderLayout.NORTH);
        bookInfo.add(content,BorderLayout.CENTER);
        bookInfo.add(bottom,BorderLayout.SOUTH);

    }
    public static void closeBookInfo(){
        bookInfo.dispose();
        SearchPane.value.setText("");
    }
    public static void buyBookSale(String message)
    {

        rBar = message.charAt(5);

        if(rBar == 'T')
        {
            JOptionPane.showMessageDialog(null,"Your EBook has been ordered");
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Your EBook transaction failed, please try again.");
        }
    }
    public static void buyBook() throws IOException {
        if(MainFrame.state == 1)
        {
            String message =  MainFrame.communicate.sendData(ID, LoginPane.custID, price, 1);
            bookInfo.dispose();
            SearchPane.value.setText("");
            MainFrame.updateStatus("Order Sent");
            buyBookSale(message);
            MainFrame.updateStatus("Logged In");
        }
        else
        {
            JOptionPane.showMessageDialog(null,"You must login or register before you can purchase an EBook.");
        }
    }

}
